const fs = require("fs");
const path = require("path");
//当前目录
const currentDir=path.dirname(__dirname)
const currentDir2=path.dirname(__dirname)


// 获取当前文件的前几级目录
const levelsToGoUp = 5; // 例如，要访问当前文件的前3级目录
const targetFolderName = 'public';
const targetFoldercontent = 'content';
// 构建目标目录路径
let folderPath = currentDir;
let folderPath2=currentDir2
for (let i = 0; i < levelsToGoUp; i++) {
  folderPath = path.dirname(folderPath);
}
for (let i = 0; i < levelsToGoUp; i++) {
  folderPath2 = path.dirname(folderPath2);
}
// 添加目标文件夹名称,灵活路径
folderPath = path.join(folderPath, targetFolderName);
//绝对路径
// const folderPath = "E:\\A_working_directory\\hugo_project\\hugo-备份\\PATH\\public"; // 替换为您要读取的文件夹路径
// 使用 fs.stat 检查文件夹是否存在
fs.stat(folderPath, (err, stats) => {
  if (err) {
    if (err.code === 'ENOENT') {
      console.error('文件夹不存在');
    } else {
      console.error('发生其他错误:', err);
    }
  } else {
    if (stats.isDirectory()) {
      console.log('文件夹存在');
    } else {
      console.error('路径存在，但不是文件夹');
    }
  }
});
// console.log(folderPath);
//文件夹名字
const basePath = path.join(folderPath2, targetFoldercontent);
console.log(basePath);
// 读取指定路径下的所有文件夹
fs.readdir(basePath, (err, files) => {
  if (err) {
    console.error(err);
    return;
  }

  // 过滤出文件夹
  const directories = files.filter(file => {
    const filePath = path.join(basePath, file);
    return fs.statSync(filePath).isDirectory();
  });
// 使用 map() 函数拼接路径
const fullPaths = directories.map(folder => path.join(basePath, folder));

// fullPaths 包含完整的文件夹路径
// console.log(fullPaths);

 fullPaths.filter(filei=>{

  fs.readdir(filei, (err, files) => {
    //要遍历的文件夹容器
const targetFolders= files.filter(file=>{
      const filePath = path.join(filei, file);
      return fs.statSync(filePath).isDirectory();
    })
    // 定义要读取的目标文件夹名称
    // const targetFolders = [ "course", "experiment", "video"];
    // const targetFolders = [  "video"];
    // 正则表达式用于匹配中文字符
    const chineseRegex = /[\u4e00-\u9fa5]/;
    // 使用fs.readdirSync同步读取文件夹内容
    try {
      targetFolders.forEach((targetFolder) => {
        const folderSubpath = path.join(folderPath, "zh", targetFolder);
        const subfolders = fs.readdirSync(folderSubpath).filter((item) => {
          const itemPath = path.join(folderSubpath, item);
          return fs.statSync(itemPath).isDirectory();
        });
    
        // console.log(`"zh/${targetFolder}" 文件夹下的子文件夹：`, subfolders);
    
        // 遍历中文子文件夹
        subfolders.forEach((subfolder) => {
          const subfolderSubpath = path.join(folderSubpath, subfolder);
          const subSubfolders = fs.readdirSync(subfolderSubpath).filter((item) => {
            const itemPath = path.join(subfolderSubpath, item);
            return fs.statSync(itemPath).isDirectory();
          });
    
          // console.log(
          //   `"zh/${targetFolder}/${subfolder}" 文件夹下的子文件夹：`,
          //   subSubfolders
          // );
    
          // 继续遍历subSubfolders
          subSubfolders.forEach((subSubfolder) => {
            const subSubfolderSubpath = path.join(subfolderSubpath, subSubfolder);
            const subSubSubfolders = fs.readdirSync(subSubfolderSubpath).filter((item) => {
              const itemPath = path.join(subSubfolderSubpath, item);
              return fs.statSync(itemPath).isDirectory();
            });
    
            // console.log(
            //   `"zh/${targetFolder}/${subfolder}/${subSubfolder}" 文件夹下的子文件夹：`,
            //   subSubSubfolders
            // );
    
            // 继续遍历subSubSubfolders，获取文件
            subSubSubfolders.forEach((subSubSubfolder) => {
              const subSubSubfolderSubpath = path.join(subSubfolderSubpath, subSubSubfolder);
              const files = fs.readdirSync(subSubSubfolderSubpath).filter((item) => {
                const itemPath = path.join(subSubSubfolderSubpath, item);
                return fs.statSync(itemPath).isFile(); // 只获取文件，不获取文件夹
              });
    
              // console.log(
              //   `"zh/${targetFolder}/${subfolder}/${subSubfolder}/${subSubSubfolder}" 文件夹下的文件：`,
              //   files
              // );
              files.forEach((i)=>{
               if( chineseRegex.test(i)){
                // console.log(i);
               }else{
                //英文文件
                // console.log(i);
                // console.log(path.join(subSubfolderSubpath, subSubSubfolder,i));
                const removefilePath=path.join(subSubfolderSubpath, subSubSubfolder,i)
                fs.unlink(removefilePath, (err) => {
                  if (err) {
                    console.error('删除文件失败', err);
                  } else {
                    console.log('文件已成功删除');
                  }
                });
               }
              })
            });
          });
        });
      });
    
      targetFolders.forEach((targetFolder) => {
        const folderSubpath = path.join(folderPath, "en", targetFolder);
        const subfolders = fs.readdirSync(folderSubpath).filter((item) => {
          const itemPath = path.join(folderSubpath, item);
          return fs.statSync(itemPath).isDirectory();
        });
    
        // console.log(`"en/${targetFolder}" 文件夹下的子文件夹：`, subfolders);
    
        // 遍历英文子文件夹
        subfolders.forEach((subfolder) => {
          const subfolderSubpath = path.join(folderSubpath, subfolder);
          const subSubfolders = fs.readdirSync(subfolderSubpath).filter((item) => {
            const itemPath = path.join(subfolderSubpath, item);
            return fs.statSync(itemPath).isDirectory();
          });
    
          // console.log(
          //   `"en/${targetFolder}/${subfolder}" 文件夹下的子文件夹：`,
          //   subSubfolders
          // );
    
          // 继续遍历subSubfolders
          subSubfolders.forEach((subSubfolder) => {
            const subSubfolderSubpath = path.join(subfolderSubpath, subSubfolder);
            const subSubSubfolders = fs.readdirSync(subSubfolderSubpath).filter((item) => {
              const itemPath = path.join(subSubfolderSubpath, item);
              return fs.statSync(itemPath).isDirectory();
            });
    
            // console.log(
            //   `"en/${targetFolder}/${subfolder}/${subSubfolder}" 文件夹下的子文件夹：`,
            //   subSubSubfolders
            // );
    
            // 继续遍历subSubSubfolders，获取文件
            subSubSubfolders.forEach((subSubSubfolder) => {
              const subSubSubfolderSubpath = path.join(subSubfolderSubpath, subSubSubfolder);
              const files = fs.readdirSync(subSubSubfolderSubpath).filter((item) => {
                const itemPath = path.join(subSubSubfolderSubpath, item);
                return fs.statSync(itemPath).isFile(); // 只获取文件，不获取文件夹
              });
    
              // console.log(
              //   `"en/${targetFolder}/${subfolder}/${subSubfolder}/${subSubSubfolder}" 文件夹下的文件：`,
              //   files
              // );
              files.forEach((i)=>{
                if( chineseRegex.test(i)){
                  // 中文文件
                //  console.log(i);
                const removefilePath=path.join(subSubfolderSubpath, subSubSubfolder,i)
                fs.unlink(removefilePath, (err) => {
                  if (err) {
                    console.error('删除文件失败', err);
                  } else {
                    console.log('文件已成功删除');
                  }
                });
                }else{
                //  console.log(i);
                }
               })
            });
          });
        });
      });
    } catch (err) {
      console.error("读取文件夹失败", err);
    }
    

  })
})
// console.log(directories2);
  // 存储文件夹名称到 targetFolders 数组中
 

  // 打印文件夹名称
  // console.log(targetFolders);
});




